package com.Pawtrack.web.controller.info;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.Pawtrack.common.annotation.Log;
import com.Pawtrack.common.annotation.DataScope;
import com.Pawtrack.common.core.controller.BaseController;
import com.Pawtrack.common.core.domain.AjaxResult;
import com.Pawtrack.common.enums.BusinessType;
import com.Pawtrack.info.domain.Pet;
import com.Pawtrack.info.service.IPetService;
import com.Pawtrack.common.utils.poi.ExcelUtil;
import com.Pawtrack.common.core.page.TableDataInfo;
import com.Pawtrack.common.utils.SecurityUtils;
import com.Pawtrack.common.utils.file.FileUploadUtils;
import com.Pawtrack.common.config.RuoYiConfig;
import com.Pawtrack.common.utils.file.MimeTypeUtils;

/**
 * 宠物信息Controller
 * 
 * @author ruoyi
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/system/pet")
public class PetController extends BaseController
{
    @Autowired
    private IPetService petService;

    /**
     * 查询宠物信息列表
     */
    @GetMapping("/list")
    @DataScope(deptAlias = "p", userAlias = "p")
    public TableDataInfo list(Pet pet)
    {
        startPage();
        List<Pet> list = petService.selectPetList(pet);
        return getDataTable(list);
    }

    /**
     * 导出宠物信息列表
     */
    @Log(title = "宠物信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Pet pet)
    {
        List<Pet> list = petService.selectPetList(pet);
        ExcelUtil<Pet> util = new ExcelUtil<Pet>(Pet.class);
        util.exportExcel(response, list, "宠物信息数据");
    }

    /**
     * 获取宠物信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(petService.selectPetById(id));
    }

    /**
     * 新增宠物信息
     */
    @Log(title = "宠物信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Pet pet)
    {
        // 设置创建人为当前用户ID
        pet.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(petService.insertPet(pet));
    }

    /**
     * 修改宠物信息
     */
    @Log(title = "宠物信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Pet pet)
    {
        // 设置更新人为当前用户ID
        pet.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(petService.updatePet(pet));
    }

    /**
     * 删除宠物信息
     */
    @Log(title = "宠物信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(petService.deletePetByIds(ids));
    }

    /**
     * 宠物照片上传
     */
    @Log(title = "宠物照片", businessType = BusinessType.UPDATE)
    @PostMapping("/photo")
    public AjaxResult uploadPhoto(@RequestParam("photofile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String photo = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("photoUrl", photo);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }
}
